package com.ytjj.quartz.service;

import com.ytjj.common.constants.QueueConstants;
import com.ytjj.common.dto.mqdto.PrepaidRefillDto;
import com.ytjj.quartz.dao.RechargeDao;
import com.ytjj.quartz.model.Recharge;
import com.ytjj.quartz.model.ReturnRechargeResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 *  *
 **/
@Service
@Slf4j
public class RechargeService {

    @Resource
    private RechargeDao rechargeDao;

    @Resource
    private RabbitTemplate rabbitTemplate;


    /**
     * 定时任务处理充值失败订单
     */
    public void rechargeTask() {
        List<Recharge> recharges = rechargeDao.findFailRecharge();
        if(recharges != null) {
            for (Recharge recharge : recharges) {
                PrepaidRefillDto prepaidRefillDto = new PrepaidRefillDto();
                prepaidRefillDto.setOrderNo(recharge.getOrderNo());
                log.debug("处理充值订单号orderNo:{}",recharge.getOrderNo());
                // 发送队列
                rabbitTemplate.convertAndSend(QueueConstants.RECHARGE_QUEUE,prepaidRefillDto);
            }
        }
    }

    /**
     * 定时任务处理返充值失败订单
     */
    public void returnRechargeTask() {
        List<ReturnRechargeResponse> responseList = rechargeDao.selectRechargeList();
        if (responseList != null) {
            for (ReturnRechargeResponse response : responseList) {
                if (response.getPhone() != null && response.getOrderNo() != null) {
                    Integer status = rechargeDao.getRechargeStatus(response.getPhone(), response.getOrderNo());
                    if (status == 1) {
                        rechargeDao.updateChargeStatus(response.getOrderinfoId(), 2);
                    }
                    if (status == 3){
                        rechargeDao.updateChargeStatus(response.getOrderinfoId(), 3);
                    }
                }
            }
        }
    }
}
